package com.ht.module.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.teautil.Common;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.config.dataAuth.DataScope;
import com.ht.constant.BusConstant;
import com.ht.module.bus.entity.TStation;
import com.ht.module.bus.entity.TVehicleRouteResponsibility;
import com.ht.module.bus.mapper.TStationMapper;
import com.ht.module.bus.mapper.TVehicleRouteResponsibilityMapper;
import com.ht.module.bus.service.TVehicleRouteResponsibilityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.bus.vo.TVehicleRouteResponsibilityAddVO;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆负责的线路信息 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@Service
public class TVehicleRouteResponsibilityServiceImpl extends ServiceImpl<TVehicleRouteResponsibilityMapper, TVehicleRouteResponsibility> implements TVehicleRouteResponsibilityService {
    @Resource
    private TStationMapper stationMapper;

    @Override
    public Pager<TVehicleRouteResponsibility> findPage(Pager<TVehicleRouteResponsibility> pager, TVehicleRouteResponsibility en) {
        QueryWrapper<TVehicleRouteResponsibility> qw = getQW(en);
        baseMapper.findPageAuth(pager, qw,null);
        handleStations(pager.getRecords());
        return pager;
    }

    @Override
    public void add(TVehicleRouteResponsibilityAddVO en) {
        List<TVehicleRouteResponsibility> list = new ArrayList<>();

        for (String routeCode : en.getRouteCodes()) {
            for (String vehicleLicenseLate : en.getVehicleLicenseLates()) {
                TVehicleRouteResponsibility row = new TVehicleRouteResponsibility()
                        .setRouteCode(routeCode)
                        .setDistrictCode(en.getDistrictCode())
                        .setVehicleLicenseLate(vehicleLicenseLate);
                CommMethod.beanCreate(row);
                list.add(row);
            }
        }
        //批量插入
        this.saveBatch(list);
    }

    @Override
    public Pager<TVehicleRouteResponsibility> findPageAuth(Pager<TVehicleRouteResponsibility> pager, TVehicleRouteResponsibility en) {
        QueryWrapper<TVehicleRouteResponsibility> qw = getQW(en);
        baseMapper.findPageAuth(pager, qw,new DataScope().initOrder(pager));
        handleStations(pager.getRecords());
        return pager;
    }

    private void handleStations(List<TVehicleRouteResponsibility> rows){
        //查询所有站点
        List<TStation> tStations = stationMapper.findListAuth(new QueryWrapper<>(),null);
        //构建途经点数据
        for (TVehicleRouteResponsibility record : rows) {
            List<String> list = new ArrayList<>(Arrays.asList(record.getPassingPoindCode().split(",")));
            record.setStations(
                    list.stream().map(x -> {
                                return tStations.stream()
                                        .filter(y -> StrUtil.equals(y.getStationCode(), x))
                                        .findFirst().orElse(null);
                            }).filter(BeanUtil::isNotEmpty)
                            .collect(Collectors.toList())
            );
        }
    }


    private QueryWrapper<TVehicleRouteResponsibility> getQW(TVehicleRouteResponsibility en) {
        QueryWrapper<TVehicleRouteResponsibility> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(en.getRouteName())) {
            qw.like("tab.route_name", en.getRouteName());
        }

        if (StrUtil.isNotEmpty(en.getDistrictCode())) {
            qw.eq("tab.district_code", en.getDistrictCode());
        }

        if (StrUtil.isNotEmpty(en.getCompanyName())) {
            qw.eq("tab.company_name", en.getCompanyName());
        }

        if (StrUtil.isNotEmpty(en.getState())) {
            qw.eq("tab.state", en.getState());
        }
        qw.eq("tab.del_flag", BusConstant.DEL_FLAG_N);
        qw.orderByDesc("tab.create_date");
        return qw;

    }
}
